﻿using System;
using System.Configuration;
using System.Collections;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace IntraVision.Web
{
    public class URLRewriteModule : IHttpModule
    {
        #region IHttpModule Members
        void IHttpModule.Dispose()
        {
        }

        void IHttpModule.Init(HttpApplication app)
        {
            app.BeginRequest += new EventHandler(RewriteUrl);
        }
        #endregion

        /// <summary>
        /// Rewrites url with the url, specified in configuration section
        /// </summary>
        /// <param name="sender">HttpApplication</param>
        /// <param name="e">EventArgs</param>
        void RewriteUrl(object sender, EventArgs e)
        {
            HttpApplication app = sender as HttpApplication;
            string url = URLHelper.GetRedirectUrl(app.Request.Url.AbsolutePath, app.Request.ApplicationPath);
            if (!string.IsNullOrEmpty(url))
            {
#if DEBUG
                app.Server.TransferRequest(url);
#else
                app.Context.RewritePath(url);
#endif
            }
        }
    }
}
